package com.edol.painting.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.View;

import com.edol.painting.R;
import com.edol.painting.painting.Brush;
import com.edol.painting.painting.Particle;
import com.edol.painting.painting.Route;
import com.edol.painting.utils.PainterUtils;

public class BrushAdjusterPreview extends View {
	
	private Brush mBrush;
	private Route mDefaultRoute;
	private Rect mBounds;
	private Bitmap mPreview;
	private Canvas mPreviewCanvas;
	private Paint mPaint;
	private Rect mTextureBounds;
	private ShapeDrawable mCicleDrawable = null;
	private Shader mShader = null;
	
	public BrushAdjusterPreview(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView();
	}

	public BrushAdjusterPreview(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	public BrushAdjusterPreview(Context context) {
		super(context);
		initView();
	}
	
	public void initView() {
		mDefaultRoute = new Route();
		int stepX = getResources().getDimensionPixelSize(R.dimen.preview_step_x);
		int stepY = getResources().getDimensionPixelSize(R.dimen.preview_step_y);
		int offX = getResources().getDimensionPixelSize(R.dimen.preview_route_left);
		int offY = getResources().getDimensionPixelSize(R.dimen.preview_route_top);
		mDefaultRoute.add(new Particle(stepX + offX, stepY * 3 + offY, 296.56f));
		mDefaultRoute.add(new Particle(stepX * 4 + offX, stepY * 4 + offY, 0));
		mDefaultRoute.add(new Particle(stepX * 7 + offX, stepY * 3 + offY, 63.4349f));
		mDefaultRoute.add(new Particle(stepX * 10 + offX, stepY * 2 + offY, 0));
		mDefaultRoute.add(new Particle(stepX * 13 + offX, stepY * 3 + offY, 296.56f));
		mDefaultRoute.add(new Particle(stepX * 16 + offX, stepY * 4 + offY, 296.56f));
		mBounds = new Rect();
		mPaint = new Paint();
		int left = getResources().getDimensionPixelSize(R.dimen.preview_texture_left);
		int top = getResources().getDimensionPixelSize(R.dimen.preview_texture_top);
		int right = getResources().getDimensionPixelSize(R.dimen.preview_texture_right);
		int bottom = getResources().getDimensionPixelSize(R.dimen.preview_texture_bottom);
		mTextureBounds = new Rect(left, top, right, bottom);
		mCicleDrawable = new ShapeDrawable(new OvalShape());
	}
	
	public void setBrush(Brush brush) {
		mBrush = Brush.copyBrush(getContext(), brush, Brush.TARGET_PREVIEW);
		mShader = null;
	}
	
    public void setBrushColor(int color) {
        if (mBrush != null) {
            mBrush.setColor(color);
        }
    }
	public void setBrushWidth(int width) {
		if (mBrush != null) {
			mBrush.setWidth(width);
		}
	}
	
	public void setBrushAlpha(int alpha) {
		if (mBrush != null) {
			mBrush.setAlpha(PainterUtils.getAlphaCurveValue(alpha));
			mCicleDrawable.getPaint().setAlpha(PainterUtils.getAlphaCurveValue(alpha));
		}
	}
	
	public void setBrushShader(Shader shader) {
		mShader = shader;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (mPreview == null) {
			mPreview = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
			mPreviewCanvas = new Canvas(mPreview);
		}
		canvas.drawColor(0xffededed);
		if (mShader != null) {
			mPreviewCanvas.drawColor(0x0, Mode.CLEAR);
			mCicleDrawable.setBounds(mTextureBounds);
			mCicleDrawable.getPaint().setShader(mShader);
			mCicleDrawable.draw(mPreviewCanvas);
			mPreviewCanvas.drawColor(mBrush.getColor(), Mode.SRC_IN);
			canvas.drawBitmap(mPreview, 0, 0, mPaint);
		} else {
			if (mBrush != null) {
				mPreviewCanvas.drawColor(0x0, Mode.CLEAR);
				mBounds.setEmpty();
				mBrush.setBrushLayer(mPreview, mPreviewCanvas);
				mBrush.beginRoute(canvas, mDefaultRoute.get(0), mBounds);
				int count = mDefaultRoute.size();
				for (int i = 1; i < count - 1; i++) {
					mBrush.drawRoute(canvas, mDefaultRoute.get(i), mBounds);
				}
				mBrush.endRoute(canvas, mDefaultRoute.get(count - 1), mBounds);
				if (!mBounds.isEmpty()) {
					mPaint.setAlpha(mBrush.getAlpha());
					canvas.drawBitmap(mPreview, 0, 0, mPaint);
				}
			}
		}
	}
	
	public void clear() {
		if (mPreview != null) {
			mPreview.recycle();
			mPreview = null;
		}
		mCicleDrawable.setCallback(null);
		mCicleDrawable = null;
	}
	
}
